//
//  ZNPickerView.m
//  YFProject
//
//  Created by Gideon on 16/4/22.
//  Copyright © 2016年 LiZhenning. All rights reserved.
//

#import "ZNPickerView.h"

#define ColorRGBA(_red,_green,_blue,_alpha) \
[UIColor colorWithRed:(_red/255.0) green:(_green/255.0) blue:(_blue/255.0) alpha:_alpha]


@interface ZNPickerView ()
<
UIPickerViewDelegate,
UIPickerViewDataSource
>
{
    
    UIView *viewPickerBg;
    UIPickerView *picker;
    NSArray *array;
    NSString *pickerString;
    NSString *pickerUnit;
    NSInteger flagSelected;
}
@end

@implementation ZNPickerView

@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame Data:(NSArray *)data Unit:(NSString *)unit {
    
    self = [self initWithFrame:frame];
    
    if (self)
    {
        array = data;
        
        pickerString = @"";
        pickerUnit = unit;
        flagSelected = 0;
        
        self.backgroundColor = ColorRGBA(22, 22, 22, 0.8);
        
        viewPickerBg = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 256)];
        [viewPickerBg setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:viewPickerBg];
        
        picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 216)];
        picker.dataSource = self;
        picker.delegate = self;
        
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
        [toolBar setUserInteractionEnabled:YES];
        toolBar.barStyle = UIBarStyleDefault;
        
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"  取消" style:UIBarButtonItemStylePlain target:self action:@selector(btnCancelPressed)];
        [left setTintColor:[UIColor blackColor]];
        
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定  " style:UIBarButtonItemStylePlain target:self action:@selector(btnDonePressed)];
        [right setTintColor:[UIColor blackColor]];
        
        [toolBar setItems:@[left,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil],right]];
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 1)];
        [viewLine setBackgroundColor:ColorRGBA(227, 227, 227, 1)];
        
        
        [viewPickerBg addSubview:toolBar];
        [viewPickerBg addSubview:viewLine];
        [viewPickerBg addSubview:picker];
        
    }
    
    return self;
}

#pragma mark - 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    
    return 1;
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (array != nil)
    {
        
        NSInteger count = [array count];
        
        return count;
    }
    
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    
    if (array != nil)
    {
        
        return [NSString stringWithFormat:@"%@%@",array[row],pickerUnit];
    }
    
    
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    pickerString = array[row];
    flagSelected = row;
    
}


#pragma mark -

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if (newSuperview == nil)
    {
        return;
    }
    
    CGRect frameBg = viewPickerBg.frame;
    
    frameBg.origin.y = self.frame.size.height - 256;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        viewPickerBg.frame = frameBg;
        
    }];
    
}

- (void)btnCancelPressed
{
    
    CGRect frameBg = viewPickerBg.frame;
    frameBg.origin.y = self.frame.size.height;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        viewPickerBg.frame = frameBg;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}

- (void)btnDonePressed
{
    
    if (_delegate && [_delegate respondsToSelector:@selector(didPickerSelected:Row:)])
    {
        NSString *string = @"";
        
        if (array)
        {
            string = array[flagSelected];
        }
        
        [_delegate didPickerSelected:string Row:flagSelected];
    }
    
    
    [self btnCancelPressed];
    
}



@end
